import 'package:flame/components.dart';
import 'package:flutter/rendering.dart';
import 'package:klondike/klondike/klondike_game.dart';
import 'package:klondike/klondike/klondike_world.dart';

import 'components/flat_button.dart';

class HomePage extends World with HasGameReference<KlondikeGame> {

  late final TextComponent _logo;
  late final FlatButton _button1;
  late final FlatButton _button2;

  static const double cardWidth = KlondikeGame.cardWidth;
  static const double cardHeight = KlondikeGame.cardHeight;
  static const double cardGap = KlondikeGame.cardGap;

  @override
  Future<void> onLoad() async {
    addAll([
      _logo = TextComponent(
        text: 'Syzygy',
        textRenderer: TextPaint(
          style: const TextStyle(
            fontSize: 64,
            color: Color(0xFFC8FFF5),
            fontWeight: FontWeight.w800,
          ),
        ),
        anchor: Anchor.center,
      ),
      _button1 = FlatButton(
        'klondike',
        size: Vector2(cardWidth, cardGap*2),
        onReleased: () async => game.world = KlondikeWorld(),
      ),
      _button2 = FlatButton(
        'spinner',
        size: Vector2(cardWidth, cardGap*2),
        onReleased: () async => game.world = KlondikeWorld(),
      ),
    ]);
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);

    _logo.position = Vector2(cardWidth, cardGap * 2);
    _button1.position = Vector2(cardWidth, cardGap * 5);
    _button2.position = Vector2(cardWidth, cardGap * 8);

    game.camera.viewfinder.visibleGameSize = 
      Vector2(cardWidth, cardHeight * 2);
    game.camera.viewfinder.position = Vector2(cardWidth, 0);
    game.camera.viewfinder.anchor = Anchor.topCenter;
  }
}
